package com.cf.r964;

import java.io.*;

public class e {
    public static void main(String[] args) throws IOException {
        final int MAX = 200_000;
        int[] dp = new int[MAX + 1];
        long[] pref = new long[MAX + 1];

        // 1. 预处理 dp 和前缀和
        dp[0] = 0;
        for (int i = 1; i <= MAX; i++) {
            dp[i] = dp[i / 3] + 1;
            pref[i] = pref[i - 1] + dp[i];
        }

        // 2. 读入并回答
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine().trim());
        StringBuilder sb = new StringBuilder();
        while (t-- > 0) {
            String[] sp = br.readLine().split(" ");
            int l = Integer.parseInt(sp[0]);
            int r = Integer.parseInt(sp[1]);
            long ans = 2L * dp[l] + (pref[r] - pref[l]);
            sb.append(ans).append('\n');
        }
        System.out.print(sb);
    }
}
